package edu.miami.group.photolog;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity {

	public static final String TABLE_NAME = "edu.miami.group.photolog.Login.TABLE_NAME";

	private LoginDatabase loginDatabase;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		loginDatabase = new LoginDatabase(this);
	}

	public void buttonClick(View view) {
		String name = ((EditText) findViewById(R.id.login_name)).getText()
				.toString();
		String password = ((EditText) findViewById(R.id.login_password))
				.getText().toString();

		if(name.equals("") || password.equals("")) {
			return;
		}
		
		boolean startNext = false;

		ContentValues user = loginDatabase.getDataByUsername(name);
		switch (view.getId()) {
		case R.id.login_login:
			// ContentValues user = loginDatabase.getDataByUsername(name);
			if (user == null) {
				Toast.makeText(this, "No User With That Name",
						Toast.LENGTH_LONG).show();
				break;
			} else {

				Toast.makeText(this, "Got User", Toast.LENGTH_LONG).show();

				if (!user.get("password").equals(password)) {
					Toast.makeText(this, "Wrong Password", Toast.LENGTH_LONG)
							.show();
				} else {
					Toast.makeText(this, "Right Password", Toast.LENGTH_LONG)
							.show();
					startNext = true;
				}
			}
			break;
		case R.id.login_signup:

			if (user == null) {
				ContentValues cv = new ContentValues();
				cv.put("username", name);
				cv.put("password", password);
				loginDatabase.addUser(cv);
				startNext = true;
				Toast.makeText(this, "User created", Toast.LENGTH_LONG)
				.show();
			} else {
				Toast.makeText(this, "User already created", Toast.LENGTH_LONG)
				.show();
			}
			break;
		}

		if (startNext) {
			Intent intent = new Intent();
			intent.setClassName("edu.miami.group.photolog",
					"edu.miami.group.photolog.MainActivity");
			intent.putExtra(TABLE_NAME, name);
			startActivity(intent);
		}
	}
}
